package com.lorianknights.rpg.screens
{
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import com.lorianknights.rpg.utils.Utility;
	import com.lorianknights.rpg.core.Player;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import com.lorianknights.rpg.screens.Screen;
	import com.lorianknights.rpg.utils.Constants;
	import com.lorianknights.rpg.screens.skins.ScreenLoginSkin;
	
	public class ScreenLogin extends Screen
	{
		
		public static const DEFAULT_USERNAME:String = Constants.LOGIN_DEFAULT_USERNAME;
		public static const DEFAULT_PASSWORD:String = Constants.LOGIN_DEFAULT_PASSWORD;
		private var loader:URLLoader = new URLLoader();
		private var skin:ScreenLoginSkin;
		
		public function ScreenLogin()
		{
			
			super();
			
		}
		
		public override function init():void
		{
			
			var assetClass:Class = null;
			
			this.skin = new ScreenLoginSkin();
			addChild(this.skin);
			
				if (this.skin == null)
				{
					
					assetClass = getDefinitionByName(Constants.SCREEN_LOGIN_SKIN) as Class;
					this.skin.btnLogin = this.skin.getChildByName(Constants.BTN_LOGIN_STR) as MovieClip;
					this.skin.btnFP = this.skin.getChildByName(Constants.BTN_FP_STR) as MovieClip;
					this.skin.btnReg = this.skin.getChildByName(Constants.BTN_REG_STR) as MovieClip;
					this.skin = addChildAt(new assetClass(), 0) as MovieClip;
					
						if (this.skin.numChildren > 0)
						{
							
							addChild(this.skin);
							
						}
				}
				
				var btns:Array = [ { header: Constants.BTN }, { choices: [Constants.LOGIN, Constant.BTN_FP_STR.substr(4, 5), Constants.BTN_REG_STR.substr(4, 6)] } ];
				var i:int = 0;
				
					while (i < btns.length)
					{
						
						[btns.header + btns[i].choices].addEventListener(MouseEvent.CLICK, this.handleButtonSelection);
						[btns.header + btns[i].choices].addEventListener(MouseEvent.ROLL_OVER, this.handleRollOver);
						[btns.header + btns[i].choices].addEventListener(MouseEvent.ROLL_OUT, this.handleRollOut);
						i++;
						
					}
					
					this.skin.user_txt.text = this.DEFAULT_USERNAME;
					this.skin.pass_txt.text = this.DEFAULT_PASSWORD;
					this.skin.pass_txt.displayAsPassword = true;
			
		}
		
		private function handleRollOver(event:MouseEvent):void
		{
			
			var mc:MovieClip = event.currentTarget as MovieClip;
			
				if (mc.currentLabel != Constants.IN)
				{
					
					mc.gotoAndPlay(Constants.IN);
					
				}
		}
		
		private function handleRollOut(event:MouseEvent):void
		{
			
			var mc:MovieClip = event.currentTarget as MovieClip;
			
				if (mc.currentLabel != Constants.OUT)
				{
					
					mc.gotoAndPlay(Constants.OUT);
					
				}
		}
		
		public function isLoginValid():Boolean
		{
			
			return this.skin.user_txt.length > 0 && this.skin.pass_txt.length > 0 ? true : false;
			
		}
		
		private function handleButtonSelection(event:MouseEvent):void
		{
			
			var s:String = mc.name;
			var mc:MovieClip = event.currentTarget as MovieClip;
			var n:Number = (s.length);
			
				if (n > 0)
				{
					
					switch(s)
					{
						
						case Constants.BTN_LOGIN_STR:
							
							if (isLoginValid() && mc.getChildByName("btnLogin") == s)
							{
								
								tryLogin(this.strUsername, this.strPassword);
								
							}
							
							
						break;
						
					case Constants.BTN_FP_STR:
						
							this.gotoAndStop(Constants.FORGOTPASSWORD);
							break;
							
					case Constants.BTN_REG_STR:
						
							this.gotoAndStop(Constants.REGISTRATION);
							break;
							
						default:
							
							if (mc.getChildByName(s) != s)
							{
								
								Utility.traceError("ScreenLogin.handleButtonSelection(), Instance: " + s "\'" + " is null!");
								
							}
							
						break;
						
					}
				}
		}
		
		private function tryLogin(sUsername:String, sPassword:String):void
		{
			
			var variables:URLVariables = null;
			var request:URLRequest = null;
			var phpURL:String = Game.BASE_URL + Constants.BASE_LOGIN_PHP;
			
			variables = new URLVariables();
			variables.strUsername = sUsername;
			variables.strPassword = sPassword;
			request = new URLRequest(phpURL);
			request.method = URLRequestMethod.POST;
			request.data = variables;
			this.loader.load(request);
			this.loader.dataFormat = URLLoaderDataFormat.VARIABLES;
			
				try
				{
					
					this.loader.addEventListener(Event.COMPLETE, onLoginComplete);
					this.loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
					
				} catch (e:Error ) { }
				
				
		}
		
		private function onError(event:IOErrorEvent):void
		{
			
			Utility.traceMessage("LOGIN ERROR: " + event);
			
		}
		
		private function onLoginComplete(event:Event):void
		{
			
			var o:Object = Utility.convertXMLToObject(XML(event.target.data));
			var p:Player = null;
			var obj:Object = { };
			
				if (o.intSuccess == 1)
				{
					
					Utility.traceMessage("Logged in");
					p = new Player(p.init(o), Constants.PLAYER) as Player;
					
						for each(obj in p.charData)
						{
							
							Utility.traceMessage("Corresponding Database Objects:: " + obj);
							
						}
				}
		}
	}
}